package cn.weekimwee.firstlineofcodeforkotlin.unit11

import android.os.Bundle
import cn.weekimwee.firstlineofcodeforkotlin.BaseActivity
import cn.weekimwee.firstlineofcodeforkotlin.R
import com.baidu.mapapi.SDKInitializer
import com.baidu.mapapi.map.BaiduMap
import com.baidu.mapapi.map.MapStatusUpdateFactory
import com.baidu.mapapi.map.MyLocationData
import com.baidu.mapapi.model.LatLng
import kotlinx.android.synthetic.main.activity_map.*

class MapActivity : BaseActivity() {
    private var lat: Double = 0.0
    private var lng: Double = 0.0
    private lateinit var baiduMap: BaiduMap
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        SDKInitializer.initialize(applicationContext)
        setContentView(R.layout.activity_map)
        lat = intent.getDoubleExtra("lat", 0.0)
        lng = intent.getDoubleExtra("lng", 0.0)

        baiduMap = mapView.map
        val update = MapStatusUpdateFactory.zoomTo(16f)
        baiduMap.animateMapStatus(update)
        val update2 = MapStatusUpdateFactory.newLatLng(LatLng(lat, lng))
        baiduMap.animateMapStatus(update2)
        baiduMap.isMyLocationEnabled = true
        baiduMap.setMyLocationData(MyLocationData.Builder().apply {
            latitude(lat)
            longitude(lng)
        }.build())
    }

    override fun onResume() {
        super.onResume()
        mapView.onResume()
    }

    override fun onPause() {
        super.onPause()
        mapView.onPause()
    }

    override fun onDestroy() {
        super.onDestroy()
        mapView.onDestroy()
        baiduMap.isMyLocationEnabled = false
    }
}
